<?php

/*
 * 锁库订单
 */

namespace Seller\Controller;

class LockOrderController extends BaseController {

    private $_model = null;

    public function _initialize() {
        parent::_initialize();
        $this->_model = M('order_goods_distribution_lock');
    }

    public function lists() {
        $where = array('customer_id' => $this->__seller_info['id']);
        $total = $this->_model->where($where)->count();
        $lists = $this->_model->where($where)->page($this->__page, $this->__row)->order('id DESC')->select();
        if ($lists) {
            foreach ($lists as $key => $value) {
                $goods_info = M('goods')->alias('g')->join('__GOODS_SUPPLY__ gs ON gs.goods_id = g.id')->join('__CUSTOMER__ c ON c.id = gs.customer_id')
                                ->where(array('gs.id' => $value['goods_supply_id']))->field('g.name,g.sku,c.shop_name')->find();
                $lists[$key] = array_merge($value, ($goods_info ? $goods_info : array()));
            }
        }
        $this->assign('lists', $lists);
        $this->__assignPage($total);
        $this->display();
    }

    /**
     * 订单详情
     */
    public function detail() {
        if (!isset($this->__get['sn']) || !preg_match('/^[0-9]+$/', $this->__get['sn']))
            $this->error('信息错误');
        $info = $this->_model->alias('ogd')->join('__GOODS_SUPPLY__ gs ON gs.id = ogd.goods_supply_id')->join('__GOODS__ g ON g.id = gs.goods_id')->join('__CUSTOMER__ c ON c.id = ogd.cusomter_supply_id')
                        ->where(array('ogd.sn' => $this->__get['sn'], 'ogd.customer_id' => $this->__seller_info['id']))->field('g.name,g.earn13,gs.lock_pay_type,gs.lock_pay_rule,c.shop_name,ogd.*,c.weixin_pay_id,c.alipay_pay_id')->find();
        if (!$info)
            $this->error('信息不存在');
        $this->assign('info', $info);
		$this->assign('local_price' , M('customer')->where(array('id' => $this->__seller_info['id']))->getField('money'));
        $this->display();
    }

    /**
     * 删除锁库订单
     */
    public function delete() {
        if (!isset($this->__get['id']) || intval($this->__get['id']) <= 0)
            $this->error('订单不存在');
        if ($this->_model->where(array('id' => $this->__get['id'], 'customer_id' => $this->__seller_info['id']))->delete()) {
            $this->success('订单删除成功');
        } else {
            $this->success('订单删除失败');
        }
    }

    /**
     * 锁库清单
     */
    public function lockStockNumList() {
        if (!isset($this->__get['goods_id']) || intval($this->__get['goods_id']) <= 0)
            tipsDivError('信息不全');
        $list = $this->_model->where(array('goods_supply_id' => $this->__get['goods_id'], 'status' => 1, 'customer_id' => $this->__seller_info['id']))
                        ->field('sn,stock_num,single_price,pay_time,total_price')->order('id DESC')->select();
        $this->assign('lists', $list);
        $this->display();
    }

}
